package com.tgjorgoski.sidekicknotes.browser;

import java.awt.Color;
import java.awt.event.*;
import java.io.File;

import javax.accessibility.Accessible;
import javax.swing.JLabel;
import javax.swing.border.Border;
import javax.swing.filechooser.FileSystemView;

import com.tgjorgoski.sidekicknotes.filedata.FileInformation;
import com.tgjorgoski.utils.DashedBorder;

public class FileLabel extends JLabel implements Accessible, FocusListener, MouseListener {
	static Border focusedBorder = new DashedBorder(Color.BLACK);
	static FileSystemView fsv = FileSystemView.getFileSystemView();

	File file;
	
	public FileLabel(File file) {
		this(file, null);
	}
	
	public FileLabel(File file, String snippet) {
		this.file=file;
		String name1 = fsv.getSystemDisplayName(file);
		if (name1.endsWith(".html")) name1=name1.substring(0, name1.length()-5);
		setText(name1);
		if (snippet!=null) {
			setToolTipText("<html>" + snippet + "</html>");
		} else {
			setToolTipText(name1);
		}

		setFocusable(true);
		addFocusListener(this);
		addMouseListener(this);
	}
	
	public File getFile() {
		return file;
	}

	public void focusGained(FocusEvent e) {
		setBorder(focusedBorder);
	}

	public void focusLost(FocusEvent e) {
		setBorder(null);
	}

	public void mouseClicked(MouseEvent e) {
		requestFocusInWindow();
	}

	public void mouseEntered(MouseEvent e) {
	}
	public void mouseExited(MouseEvent e) {
	}
	public void mousePressed(MouseEvent e) {
	}
	public void mouseReleased(MouseEvent e) {
	}

	@Override
	public boolean equals(Object obj) {
		if (!(obj instanceof FileLabel)) return false;
		FileLabel otherLabel = (FileLabel)obj;
		return otherLabel.file.equals(file);
	}

	@Override
	public int hashCode() {
		return file.hashCode();
	}
	
	FileInformation fi=null;
	public FileInformation getFileInformation() {
		if (fi==null) {
			fi=FileInformation.getFileInformation(getFile());
		}
		return fi;
	}

}
